import { createRouter, createWebHistory } from 'vue-router'
import MainLayout from '@/layout/main.vue'
import Tablemanage from '@/layout/main.vue'
import { setRoute } from '@/utils/storage'
import pinia from '@/stores/index'
import { useUserStore } from '@/stores/user'


const userStore = useUserStore(pinia)

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/login',
            component: () => import('../views/login/index.vue')
        },
        {
            path: '/register',
            component: () => import('../views/register/index.vue')
        },
        {
            path: '/',
            component: MainLayout,
            children: [
                {
                    path: '',
                    component: () => import('../views/worktable/index.vue')
                }
            ]
        },
        {
            path: '/',
            redirect: '/tablemanage',
            component: Tablemanage,
            name: "Tablemanage",
            children: [
                {
                    path: 'table',
                    name: "table",
                    component: () => import('../views/tablemanage/table/table.vue')
                },
                {
                    path: 'tablecut',
                    name: "tablecut",
                    component: () => import('../views/tablemanage/tablecut/tablecut.vue')
                }, {
                    path: 'tableseat',
                    name: "tableseat",
                    component: () => import('../views/tablemanage/tableseat/tableseat.vue')
                },
            ]
        },
        {
            //菜品管理
            path: '/menu_management',
            component: MainLayout,
            children: [
                {
                    path: '',
                    name: 'menuManagement',
                    component: () => import('../views/menuManagement/index.vue')
                }
            ]
        },
        {
            path: '/fenleigl',
            component: MainLayout,
            children: [
                {
                    path: '',
                    component: () => import('../views/caipin/fenleigl/index.vue')
                },
            ]
        },
        {
            path: '/staff',
            component: MainLayout,
            children: [
                {
                    path: '',
                    component: () => import('../views/staff/index.vue')
                },
                {
                    path: 'addstaff',
                    name: 'addstaff',
                    component: () => import('../views/staff/addStaff.vue')
                },
            ]
        },
        // 订单统计
        {
            path: '/',
            redirect: '/order',
            component: MainLayout,
            name: "Order",
            children: [

                {
                    path: 'orderStatis',
                    name: "orderStatis",
                    component: () => import('../views/order/orderStatis/index.vue')
                }, {
                    path: 'orderList',
                    name: "orderList",
                    component: () => import('../views/order/orderList/index.vue')
                }, {
                    path: 'SimulatedOrderPlacing',
                    name: "SimulatedOrderPlacing",
                    component: () => import('../views/order/SimulatedOrderPlacing/index.vue')
                }
            ]
        },
        // 商户管理
        {
            path: '/',
            redirect: '/BusinessManagement',
            component: MainLayout,
            name: "businessManagement",
            children: [
                {
                    path: 'management',
                    name: "management",
                    component: () => import('../views/BusinessManagement/management/index.vue')
                }, {
                    path: 'MerchantsAudit',
                    name: "MerchantsAudit",
                    component: () => import('../views/BusinessManagement/MerchantsAudit/index.vue')
                }
            ]
        },
        {// 账号设置
            path: '/accountset',
            component: MainLayout,
            children: [
                {
                    path: '',
                    component: () => import('../views/accountset/index.vue')
                },
            ]
        },
    ]
})

// 登录
const whiteList = ['/login', '/register']

router.beforeEach(async (to, from, next) => {
    const token = userStore.token
    if (token) {
        const hasRole = userStore.userInfo?.name
        if (hasRole) {
            // 状态保持
            if (to.path == '/staff/addstaff') {
                setRoute('/staff')
            } else {
                setRoute(to.path)
            }
            next()
        } else {
            await userStore.getUserInfo(token)
            // 状态保持
            if (to.path == '/staff/addstaff') {
                setRoute('/staff')
            } else {
                setRoute(to.path)
            }
            next()
        }
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            router.push('/login')
        }
    }

})

export default router
